package com.njsoft.mydemotest;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;


public class GoodAttributeAdapter extends RecyclerView.Adapter<GoodAttributeAdapter.MyHolder> {
    private Context mContext;
    private List<GoodAttributeBean> AttributList;
    private List<GoodHadSelectBean> HadSelectGoodAttributList = new ArrayList<>();//已经选择的数据列表
    private String TAG = "-->GoodAttribut:";
    private String GoodAttribute;

    public GoodAttributeAdapter(Context mContext, List<GoodAttributeBean> AttributList) {
        this.mContext = mContext;
        this.AttributList = AttributList;
        NewDataList();//构建添加购物车数据
    }

    /**
     * 构建需要反馈的数据
     */
    private void NewDataList() {
        for (int i = 0; i < AttributList.size(); i++) {
            GoodHadSelectBean bean = new GoodHadSelectBean();
            bean.setGoodClassAttributeName(AttributList.get(i).getGoodAttributeName());
            HadSelectGoodAttributList.add(bean);//添加名称
        }
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.good_attribute_item, parent, false);
        MyHolder holder = new MyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyHolder holder, final int position) {
        if (AttributList.size() > 0) {
            GoodAttributeBean bean = AttributList.get(position);
            final GoodHadSelectBean mHadSelectGoodBean = HadSelectGoodAttributList.get(position);
            String GoodClassAttributName = bean.getGoodAttributeName();
            holder.GoodAttributeName_TV.setText(GoodClassAttributName);

            holder.GoodAttributeList_FlowLayout.setAdapter(new TagAdapter<String>(bean.getGoodAttributList()) {
                @Override
                public View getView(FlowLayout parent, int position, String s) {
                    LayoutInflater mInflater = LayoutInflater.from(mContext);
                    TextView tv = (TextView) mInflater.inflate(R.layout.tv,
                            holder.GoodAttributeList_FlowLayout, false);
                    tv.setText(s);
                    return tv;
                }
            });

            holder.GoodAttributeList_FlowLayout.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
                @Override
                public void onSelected(Set<Integer> selectPosSet) {
                    String selectString = selectPosSet.toString();
                    selectString = selectString.substring(1, selectString.length() - 1);
                    if (!TextUtils.isEmpty(selectString)) {
                        int p = Integer.valueOf(selectString);
                        mHadSelectGoodBean.setGoodAttribute(AttributList.get(position).getGoodAttributList().get(p));
                    } else {
                        mHadSelectGoodBean.setGoodAttribute("");
                    }
                }
            });
        }
    }

    /**
     * 返回已经选择的数据列表
     *
     * @return
     */
    public List<GoodHadSelectBean> getHadSelectGoodAttributList() {
        return HadSelectGoodAttributList;
    }

    @Override
    public int getItemCount() {
        return AttributList.size();
    }

    public class MyHolder extends ViewHolder {
        private TextView GoodAttributeName_TV;
        private TagFlowLayout GoodAttributeList_FlowLayout;

        public MyHolder(View itemView) {
            super(itemView);
            GoodAttributeName_TV = itemView.findViewById(R.id.GoodAttributName);
            GoodAttributeList_FlowLayout = itemView.findViewById(R.id.GoodAttribute_flowlayout);
        }
    }
}
